﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CsharpTpl.model;
using Lephone.Data;
using System.Data.SqlClient;
using System.Threading;
using SkinSharp;

namespace CsharpTpl.window
{
    public partial class FrmLogin : Form
    {
        private Thread BeginInvokeThread;
        private delegate void LoginSuccessDelegate();
        private delegate void LoginFailedDelegate();
        public static SkinH_Net skin = new SkinH_Net();
        public static long CZYID;

        public FrmLogin()
        {
            skin.Attach();
            InitializeComponent();
            button1.Click += new EventHandler(checkYH);
            textBox1.KeyDown += new KeyEventHandler(loadMC);
            textBox3.KeyDown += new KeyEventHandler(login);
            DbEntry.Context.GetDatabaseTime();
            
        }
        private void login(object sender, KeyEventArgs args)
        {
            if (args.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }
        private void loadMC(object sender, KeyEventArgs args)
        {
            if (args.KeyCode == Keys.Enter)
            {
                T_CZY czy = T_CZY.FindOne(CK.K["DH"] == textBox1.Text.Trim());
                if (czy == null)
                {
                    textBox2.Text = "";
                    textBox1.Text = "";
                    textBox3.Text = "";
                    MessageBox.Show("用户代号不存在");
                }
                else
                {
                    textBox2.Text = czy.MC;
                    textBox3.Focus();
                }
            }
        }
        private void checkYH(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入用户名");
                textBox2.Focus();
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请输入密码");
                textBox3.Focus();
                return;
            }
            button1.Enabled = false;
            BeginInvokeThread = new Thread(new ThreadStart(yzyh));
            BeginInvokeThread.Start();
        }
        public void yzyh()
        {

            T_CZY czy = T_CZY.FindOne(CK.K["MC"] == textBox2.Text.Trim() && CK.K["MM"] == textBox3.Text.Trim());
            if (czy == null)
            {
                BeginInvoke(new LoginSuccessDelegate(loginFailed));
            }
            else
            {
                CZYID = czy.Id;
                BeginInvoke(new LoginSuccessDelegate(showMainForm));
            }

        }

        private void showMainForm()
        {
            this.Hide();
            FrmMain f1 = new FrmMain();
            f1.label1.Text = "欢迎你，" + textBox2.Text;
            f1.Show();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(BeginInvokeThread!=null && BeginInvokeThread.IsAlive)
                BeginInvokeThread.Abort(); 
            Application.Exit();
        }
        public void loginFailed()
        {
            MessageBox.Show("用户名或密码错误");
            textBox2.Focus();
            button1.Enabled = true;
        }
    }
}
